'''
Created on 2011-11-28

@author: stuart
'''
import numpy

# spring constant
k = 300.0

class Edge():
    
    def __init__(self, vertex1, vertex2):
        self.v1 = vertex1
        self.v2 = vertex2        
        
        # get the initial distance between the two vertices, this is the constraint
        vDiff = vertex2.getPos() - vertex1.getPos()
        self.initDist = numpy.sqrt(numpy.vdot(vDiff, vDiff))
        
    def solveContraints(self, dT):
        # need to adjust positions of vertices to satisfy the dist between between them
        curVDiff = self.v2.getPos() - self.v1.getPos()
        curDist = numpy.sqrt(numpy.vdot(curVDiff, curVDiff))
        
        # Hooke's law: F = -kx = m*a
        deform = curDist - self.initDist
        
        # calculate the spring force in direction v2-v1
        # apply to v1, apply the opposite foce to v1
        springForce = k * deform * curVDiff
        self.v1.applyForce(0.5 * springForce, dT)
        self.v2.applyForce(0.5 * -springForce, dT)
